<?php

namespace app\controllers;
use app\models\Advertisement;
use Yii;
use yii\web\Controller;


class AdvertisementController extends Controller
{
     public $enableCsrfValidation = false;

     public function actionIndex()
     {
         $model = new Advertisement();
/*
         $advertisementList = (new \yii\db\Query())
             ->select(['cateId', 'corporateName', 'AdvertisingImages'])
             ->from('advertisement')
             ->orderBy('cateId desc')
             ->where(['cateId' => '2'])
             ->limit(10)
             ->all();
  */
         $advertisementList = Advertisement::find()
             ->select(['cateId', 'corporateName', 'AdvertisingImages'])
             ->orderBy('cateId ASC')
//             ->where(['cateId' => '9'])
             ->limit(10)
//             ->offset(3)
//             ->groupBy('cateId')
//               ->having(['cateId'=>9])
             ->asArray()
             ->all();

         return $this->render('index', [
             'advertisementList' => $advertisementList,
         ]);
     }
     public function actionAdd()
     {
         Yii::info('aaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
         if (Yii::$app->request->isPost) {
             $postParams = Yii::$app->request->post('Advertisement');
             $m= new Advertisement();
             Yii::info($postParams);
             //$m->corporateName=$postParams['corporateName'];
             //$m->AdvertisingImages=$postParams['AdvertisingImages'];
             //$m->createTime=time();
             $data = [
                 'corporateName' => $postParams['corporateName'],
                 'AdvertisingImages' => $postParams['AdvertisingImages'],
                 'createTime' => time()
             ];

             $m->setAttributes($data);

             $m->save();
             if (!empty($m->getErrors())) {
                 Yii::error($m->getErrors());
             }
             //var_dump($m->getErrors());exit;
             return $this->redirect(['advertisement/index']);
         }
         return $this->render('form',[
             'model'=>new Advertisement(),
         ]);
     }

     public function actionUpdate()
     {
         $id = Yii::$app->request->get('id');
         Yii::info("这次修改的ID是：{$id}");

         $result=Advertisement::find()->where(['cateId'=>$id])->one();

         if ($this->request->isPost) {

             $corporateName=Yii::$app->request->post('corporateName');
             $AdvertisingImages=Yii::$app->request->post('AdvertisingImages');
             Yii::info("这次修改的信息是：{$corporateName} $AdvertisingImages ");

             $data = [
                 'corporateName'=>$corporateName,
                 'AdvertisingImages'=>$AdvertisingImages
             ];

             Advertisement::updateAll($data,['cateId'=>$id]);
             /*
             $data = [
                 'corporateName' => $corporateName,
                 'AdvertisingImages' => $AdvertisingImages,
             ];
             $result->setAttributes($data);
             $result->save();
            */
             return $this->redirect(['advertisement/index']);
         }
         return $this->render('update',[
             'model'=>$result,2
         ]);
     }
     public function actionDel(){
         $id = Yii::$app->request->get('id');
         Advertisement::deleteAll(['cateId'=>$id]);
         $this->redirect(['advertisement/index']);
     }
}